
package com.insigma.business.qrcode.common.converter;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;

import com.insigma.business.qrcode.common.dto.GovOrgBSearcher;
import com.insigma.business.qrcode.soldier.dto.GovOrgDTO;
import com.insigma.business.qrcode.soldier.entity.GovOrgDO;
import com.insigma.business.qrcode.soldier.request.GovOrgInsertRequest;
import com.insigma.business.qrcode.soldier.request.GovOrgSearchRequest;
import com.insigma.business.qrcode.soldier.request.GovOrgUpdateRequest;
import com.insigma.business.qrcode.common.converter.AbstractConverter;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.BeanUtils;
import org.springframework.util.CollectionUtils;

import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Map;


public class GovOrgProviderConverter extends AbstractConverter<GovOrgDTO, GovOrgDO> {

    private final static GovOrgProviderConverter govOrgConverter = new GovOrgProviderConverter();

    private GovOrgProviderConverter() {
    }

    
    public static GovOrgBSearcher convertRequestSO2Searcher(final GovOrgSearchRequest request) {
        final GovOrgBSearcher govOrgBSearcher = new GovOrgBSearcher(request.getPageNo(),
                request.getPageSize(), request.getSortKeyValueJoint());
        BeanUtils.copyProperties(request, govOrgBSearcher);
        return govOrgBSearcher;
    }

    
    public static GovOrgDO convertRequestUO2DO(final GovOrgUpdateRequest govOrgUpdateRequest) {
        final GovOrgDO govOrgDO = new GovOrgDO();
        BeanUtils.copyProperties(govOrgUpdateRequest, govOrgDO);
        
        return govOrgDO;
    }

    
    public static GovOrgDO convertRequestIO2DO(final GovOrgInsertRequest govOrgInsertRequest) {
        final GovOrgDO govOrgDO = new GovOrgDO();
        BeanUtils.copyProperties(govOrgInsertRequest, govOrgDO);
        return govOrgDO;
    }

    
    public static List<GovOrgDTO> convertResponseDOList2DTOList(final List<GovOrgDO> govOrgDOList) {
        return govOrgConverter.convertGenericResponseDOList2DTOList(govOrgDOList);
    }

    
    public static GovOrgDTO convertResponseDO2DTO(final GovOrgDO govOrgDO) {
        return govOrgConverter.convertGenericResponseDO2DTO(govOrgDO);
    }

    public static GovOrgDTO buildGovTree(List<GovOrgDO> search) {
        return buildGovTree(search, null);
    }

    public static GovOrgDTO buildGovTree(List<GovOrgDO> search, String fatherId) {
        Collections.sort(search, new Comparator<GovOrgDO>() {
            @Override
            public int compare(GovOrgDO o1, GovOrgDO o2) {
                return o1.getId().compareTo(o2.getId());
            }
        });
        GovOrgDTO root = null;

        Map<String, GovOrgDTO> govMap = Maps.newLinkedHashMap();
        List<GovOrgDTO> govOrgDTOS = Lists.newArrayList();
        for (GovOrgDO govOrgDO : search) {
            GovOrgDTO govOrgDTO = govOrgConverter.doConvertResponseDO2DTO(govOrgDO);
            govMap.put(govOrgDTO.getId(), govOrgDTO);

        }

        for (GovOrgDTO govOrgDTO : govMap.values()) {
            String parentOrgId = govOrgDTO.getParentOrgId();
            if (Strings.isNotBlank(parentOrgId)) {
                GovOrgDTO father = govMap.get(parentOrgId);
                if (father != null) {
                    father.getChildGov().add(govOrgDTO);
                }
            } else {
                root = govOrgDTO;
            }
        }
        
        if (root == null) {
            for (Map.Entry<String, GovOrgDTO> orgDTOEntry : govMap.entrySet()) {
                if (Strings.isNotBlank(fatherId)) {
                    if (fatherId.equals(orgDTOEntry.getValue().getId())) {
                        return orgDTOEntry.getValue();
                    }
                } else {
                    if (!CollectionUtils.isEmpty(orgDTOEntry.getValue().getChildGov())) {
                        return orgDTOEntry.getValue();
                    }
                }
            }
        }

        return root;
    }

    
    @Override
    protected GovOrgDTO doConvertResponseDO2DTO(GovOrgDO govOrgDO) {
        final GovOrgDTO govOrgDTO = new GovOrgDTO();
        try {
            BeanUtils.copyProperties(govOrgDO, govOrgDTO);
        } catch (RuntimeException be) {
            
            return null;
        }
        return govOrgDTO;
    }

}
